package com.sayi.dzapi.controller.captcha;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.resource.ResourceStore;
import cloud.tianai.captcha.resource.common.model.dto.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class CaptchaResourceConfiguration {

    public List<String> captchaTypes(){
        List<String> captchaTypes=new ArrayList<>();
        captchaTypes.add(CaptchaTypeConstant.SLIDER);
        captchaTypes.add(CaptchaTypeConstant.ROTATE);
        captchaTypes.add(CaptchaTypeConstant.CONCAT);
        captchaTypes.add(CaptchaTypeConstant.WORD_IMAGE_CLICK);
        return captchaTypes;
    }

    public List<String> listCaptchaImages() throws IOException {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        org.springframework.core.io.Resource[] resources = resolver.getResources("classpath:/captcha_images/*");

        List<String> fileNames = new ArrayList<>();
        for (org.springframework.core.io.Resource resource : resources) {
            if (resource.isReadable()) {
                // 获取文件名
                String filename = resource.getFilename();
                fileNames.add(filename);
            }
        }

        return fileNames;
    }

    private final ResourceStore resourceStore;

    @PostConstruct
    public void init() {
        /*
        //添加自定义背景图片
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/a.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/b.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/c.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/d.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/e.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/g.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/h.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/i.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/j.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.ROTATE, new Resource("classpath", "bgimages/48.jpg", "default"));
        resourceStore.addResource(CaptchaTypeConstant.WORD_IMAGE_CLICK, new Resource("classpath", "bgimages/c.jpg", "default"));

        resourceStore.addResource(CaptchaTypeConstant.SLIDER,new Resource("classpath", "captcha_images/91109819_p0_master1200.jpg","default"));
        resourceStore.addResource(CaptchaTypeConstant.SLIDER,new Resource("classpath", "captcha_images/91109819_p0_master1200.jpg","default"));
        */
        try {
            for(String captchaType:captchaTypes()) {
                for (String filenames : listCaptchaImages()) {
                    resourceStore.addResource(captchaType, new Resource("classpath", "captcha_images/" + filenames, "default"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
